package com.usercar.controller;

import com.dk.bean.Result;
import com.dk.em.ResultEnum;
import com.usercar.pojo.Label;
import com.usercar.pojo.MessageLabel;
import com.usercar.service.ILabelService;
import com.usercar.service.IMessageLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Set;

@ResponseBody
@RequestMapping("changeFace")
@Api("千人千面统一接口")
@Component
@Configurable
@EnableScheduling
public class ChangeFaceController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IMessageLabelService iMessageLabelService;

    @Autowired
    private ILabelService iLabelService;

    @ApiOperation("获取用户标签")
    @GetMapping("/{carMessageId}")
    public Result  getUserLabel(@PathVariable("carMessageId")Long carMessageId){

        //获取当前线程请求上下文类
        // RequestContext currentContext = RequestContext.getCurrentContext();
        //获取request域对象
        //HttpServletRequest request = currentContext.getRequest();
        //获取参数
        //String userName = request.getParameter("userName");
        //String token = request.getHeader("token");
        String token = "123";
        //1,在用户浏览产品时，我们会将产品单个标签存放到redis中，使用redis的zset数据
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());

        List<MessageLabel> list = iMessageLabelService.selectByMessageId(carMessageId);
        String timeKey = token+"-time";
        String carLableKey = token+"-num";
        BoundZSetOperations  zSetNum = redisTemplate.boundZSetOps(carLableKey);
        BoundZSetOperations zSetTime = redisTemplate.boundZSetOps(timeKey);
        for (MessageLabel messageLabel : list) {
            Label l = iLabelService.selectById(messageLabel.getLabel());
            String labelName = l.getLabelName();
            zSetNum.incrementScore(labelName,1D);
            int i =(int) (System.currentTimeMillis() / 1000);
            zSetTime.incrementScore(labelName,Double.valueOf(i));
        }
        return Result.buildResult(ResultEnum.SUCCESS);

    }

    @Scheduled(cron = "0 30 * * * * ")
    public void selectNumAnd(){
        //获取当前线程请求上下文类
        //RequestContext currentContext = RequestContext.getCurrentContext();
        //获取request域对象
        //HttpServletRequest request = currentContext.getRequest();
        //获取参数
        //String userName = request.getParameter("userName");
       // String token = request.getHeader("token");
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        String token = "123";
        String carLableKey = token+"-num";
        String timeKey = token+"-time";

        BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps(timeKey);
        Set<ZSetOperations.TypedTuple<String>> redisValues=boundZSetOperations.reverseRangeWithScores(0, 2);
        for (ZSetOperations.TypedTuple<String> redisValue : redisValues) {
            String value = redisValue.getValue();
            String key = token + ":";
            redisTemplate.opsForZSet().add(key,value,3D);
            ZSetOperations zSetOperations = redisTemplate.opsForZSet();
            Double score = zSetOperations.score(carLableKey, value);
            if(score<3.0){
                zSetOperations.remove(carLableKey,value);
            }
        }
        BoundZSetOperations boundZSet = redisTemplate.boundZSetOps(carLableKey);
        Set<ZSetOperations.TypedTuple<String>> set1 = boundZSet.reverseRangeWithScores(0, 2);
        int i = 3;
        for (ZSetOperations.TypedTuple<String> stringTypedTuple : set1) {
            String value = stringTypedTuple.getValue();
            int i1 = --i;
            Double aDouble = Double.valueOf(String.valueOf(i1));
            String key = token + ":";
            redisTemplate.opsForZSet().incrementScore(key,value,aDouble);
        }
    }

}
